package com.example.sls.dao.social;

import com.example.sls.pojo.social.UserGroups;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户群组表(UserGroups)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:51
 */
public interface UserGroupsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from user_groups where id=#{id}")
  UserGroups queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param userGroups 查询条件
   * @param pageSize   分页对象
   * @param offset     偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from user_groups where 1=1" +
          " and (id=#{userGroups.id} or (isnull(#{userGroups.id}) or #{userGroups.id}=0))" +
          " and (user_id=#{userGroups.userId} or (isnull(#{userGroups.userId}) or #{userGroups.userId}=0))" +
          " and (group_id=#{userGroups.groupId} or (isnull(#{userGroups.groupId}) or #{userGroups.groupId}=0))" +
          " and (role=#{userGroups.role} or (isnull(#{userGroups.role}) or #{userGroups.role}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<UserGroups> queryAllByLimit(UserGroups userGroups, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from user_groups")
  long count();

  /**
   * 新增数据
   *
   * @param userGroups 实例对象
   * @return 影响行数
   */
  @Insert("insert into user_groups (id,user_id,group_id,role) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{groupId}!=0 and !isnull(#{groupId}),#{groupId},null)," +
          "if(#{role}!='' and !isnull(#{role}),#{role},null)" +
          ")")
  int insert(UserGroups userGroups);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param userGroups 实例对象
   * @return 影响行数
   */
  @Update("update user_groups set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "group_id=if(isnull(#{groupId}) or #{groupId}=0,group_id,#{groupId})," +
          "role=if(isnull(#{role}) or #{role}='',role,#{role})" +
          " where id=#{id}")
  int update(UserGroups userGroups);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from user_groups where id=#{id}")
  int deleteById(Integer id);
}

